package com.js.mytransitiondemo.after;

// Created by JS on 2021/7/6.

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.transition.Explode;
import android.transition.Fade;
import android.transition.Slide;
import android.view.Gravity;
import android.view.View;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.js.mytransitiondemo.R;
import com.js.mytransitiondemo.adapter.EffectListAdapter;
import com.js.mytransitiondemo.databinding.ActivityAfterBinding;
import com.js.mytransitiondemo.model.Effect;

import java.util.ArrayList;
import java.util.List;

public class AfterActivity extends AppCompatActivity {

    private ActivityAfterBinding binding;
    private List<Effect> mListData = new ArrayList<>();
    private EffectListAdapter mAdapter;
    private int type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getIntExtra("type", 1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            initTransitionAnimation();
        }
        binding = ActivityAfterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    private void initTransitionAnimation() {
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); // 设置启用窗口过渡属性;
        getWindow().setAllowEnterTransitionOverlap(false); // 就是禁止交叉，只有退出过渡动画结束后才会再显示进入过渡动画。

        if (type == 1) {
            Slide slide = new Slide();
            slide.setDuration(300);
            slide.setSlideEdge(Gravity.END);
            // excludeTarget 方法将状态栏和导航栏排除在过渡动画效果之外。否则会跟着一起起动画效果，不是很美观。
            slide.excludeTarget(android.R.id.statusBarBackground, true);
            slide.excludeTarget(android.R.id.navigationBarBackground, true);

            getWindow().setExitTransition(slide);
            getWindow().setEnterTransition(slide);
            getWindow().setReenterTransition(slide);
        } else if (type == 2) {
            Explode explode = new Explode();
            explode.setDuration(300);
            // excludeTarget 方法将状态栏和导航栏排除在过渡动画效果之外。否则会跟着一起起动画效果，不是很美观。
            explode.excludeTarget(android.R.id.statusBarBackground, true);
            explode.excludeTarget(android.R.id.navigationBarBackground, true);

            getWindow().setExitTransition(explode);
            getWindow().setEnterTransition(explode);
            getWindow().setReenterTransition(explode);
        } else if (type == 3) {
            Fade fade = new Fade();
            fade.setDuration(300);
            // excludeTarget 方法将状态栏和导航栏排除在过渡动画效果之外。否则会跟着一起起动画效果，不是很美观。
            fade.excludeTarget(android.R.id.statusBarBackground, true);
            fade.excludeTarget(android.R.id.navigationBarBackground, true);

            getWindow().setExitTransition(fade);
            getWindow().setEnterTransition(fade);
            getWindow().setReenterTransition(fade);
        }
    }

    private void init() {
        initListData();
        mAdapter = new EffectListAdapter(this, mListData, new EffectListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Effect effect, View view) {
                if (TextUtils.equals(effect.getName(), "makeCustom")) {

                } else if (TextUtils.equals(effect.getName(), "makeScaleUp")) {

                } else if (TextUtils.equals(effect.getName(), "makeThumbnailScaleUp")) {

                } else if (TextUtils.equals(effect.getName(), "makeClipReveal")) {

                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        Intent intent = new Intent(AfterActivity.this, AfterTwoActivity.class);
                        intent.putExtra("effect", effect);
                        startActivity(intent,
                                ActivityOptions.makeSceneTransitionAnimation(AfterActivity.this).toBundle());
                    }
                }
            }
        });
        binding.list.setLayoutManager(new LinearLayoutManager(this));
        binding.list.setAdapter(mAdapter);
    }

    private void initListData() {
        mListData.add(new Effect("Explode", R.mipmap.img1));
        mListData.add(new Effect("Slide", R.mipmap.img2));
        mListData.add(new Effect("Fade", R.mipmap.img3));
        mListData.add(new Effect("makeCustom", R.mipmap.img4));
        mListData.add(new Effect("makeScaleUp", R.mipmap.img5));
        mListData.add(new Effect("makeThumbnailScaleUp", R.mipmap.img6));
        mListData.add(new Effect("makeClipReveal", R.mipmap.img1));
    }
}
